package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.api.net.vo.Version
import de.baumann.browser.iview.IinitView
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.*

class InitPresenter : FreeLicencePresenter<IinitView>() {
    fun updateVersion() {
        UserModel().updateVersion()
                .subscribe(object : OdinObserver<Version>() {
                    override fun onSuccess(data: Version?) {
                        if (view != null && data != null) {
                            view.update(data)
                        }
                    }
                })
    }

    private val userModel: UserModel = UserModel()
    fun login() {
        userModel.login(getMoneyAddress(), "")
                .subscribe(object : OdinObserver<LoginInfo>(view) {
                    override fun onSuccess(loginInfo: LoginInfo) {
                        saveUser(loginInfo)
                        UnicornUtil.setUserInfo(loginInfo)
                        saveInviteCode(loginInfo.inviteCode)
                        saveInviteCodeStatus(loginInfo.inviteId)
                        saveFormAddress(loginInfo.fromAddress)
                    }
                })
    }
}